import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../EcyRouter.dart';
import '../../constant/EcyEventConsnt.dart';
import '../../ecycommon/EcyEventBus.dart';
import '../../ecycommon/utils/EcyUserUtil.dart';
import 'EcyConsignmentListPage.dart';

class EcyConsignmentMainPage extends StatefulWidget {
  EcyConsignmentMainPage({super.key});

  late _EcyConsignmentMainPageState state;

  @override
  State<StatefulWidget> createState() {
    state = _EcyConsignmentMainPageState();
    return state;
  }
}

class _EcyConsignmentMainPageState extends State<EcyConsignmentMainPage> {
  _EcyConsignmentMainPageState();

  final EcyConsignmentListPage ecyConsignmentListPage = EcyConsignmentListPage(
    "",
    false,
    key: null,
  );

  reloadData() async {
    ecyConsignmentListPage.reloadData("");
  }

  @override
  void initState() {
    super.initState();

    initNotis();
  }

  initNotis() {
    EcyEventBus().on(EcyEventConst.kHttpConnected, (arg) {
      reloadData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0XFFF5F5F5),
      appBar: AppBar(
          backgroundColor: Colors.white,
          automaticallyImplyLeading: false,
          scrolledUnderElevation: 0,
          titleSpacing: 0,
          title: GestureDetector(
            onTap: () {
              EcyRouter.pushNoParams(context, EcyRouter.consignmentSearchPage);
            },
            child: Container(
              width: MediaQuery.of(context).size.width - 50.w,
              margin: const EdgeInsets.only(left: 10, right: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(8.r)),
                color: const Color(0x0A000000), //设置背景色为黑色
              ),
              height: 36.w,
              child: Center(
                  child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image(
                      image:
                          const AssetImage('assets/images/ic_home_search.png'),
                      width: 24.w,
                      height: 24.w),
                  const Text(
                    '搜索',
                    style: TextStyle(
                        fontFamily: "PingFang SC",
                        fontWeight: FontWeight.w400,
                        fontSize: 16,
                        color: Color(0xFF8C8C8C)),
                  ),
                ],
              )),
            ),
          ),
          actions: [
            PopupMenuButton(
                elevation: 0,
                offset: const Offset(0, 45),
                color: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                padding: EdgeInsets.zero,
                menuPadding: EdgeInsets.zero,
                itemBuilder: (context) => [
                      PopupMenuItem(
                          value: '0',
                          padding: EdgeInsets.zero,
                          child: const Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image(
                                  image:
                                      AssetImage('assets/images/ic_edit.png'),
                                  width: 20),
                              Text('发闲置'),
                            ],
                          ),
                          onTap: () async {
                            var loginFlag = await EcyUserUtil.logined();
                            if (loginFlag) {
                              EcyRouter().push(
                                  context,
                                  EcyRouter.consignmentPublishPage,
                                  {}).then((val) =>
                                    val ? reloadData() : null);
                            } else {
                              EcyRouter.push(
                                  context, EcyRouter.passwordLoginPage, {
                                EcyRouter.kTargetUrl:
                                    EcyRouter.consignmentPublishPage,
                              });
                            }
                          }),
                      PopupMenuItem(
                        value: '1',
                        padding: EdgeInsets.zero,
                        child: const Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Image(
                                image:
                                    AssetImage('assets/images/ic_delete.png'),
                                width: 20),
                            Text('草稿箱'),
                          ],
                        ),
                        onTap: () {
                          EcyRouter.pushWithNeedLogin(
                              context,
                              EcyRouter.myConsignmentPublishListPage,
                              {EcyRouter.kTargetUrl: ''});
                        },
                      ),
                    ],
                child: Padding(
                  padding: const EdgeInsets.only(right: 10),
                  child: Image(
                    image:
                        const AssetImage('assets/images/ic_group_publish.png'),
                    width: 24.w,
                    height: 24.w,
                  ),
                )),
          ]),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: ecyConsignmentListPage,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void deactivate() {
    super.deactivate();
  }
}
